package com.conceal.client.utils;

import com.conceal.client.utils.SimpleResponse;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Https 工具
 */
public class HttpsUtil {
    public static SimpleResponse httpsGet(String url) throws IOException {

        return httpsGet(url, "GET");
    }

    public static SimpleResponse httpsGet(String url,String method) throws IOException {
        URL obj = new URL(url);
        HttpsURLConnection conn = (HttpsURLConnection)obj.openConnection();
        conn.setRequestMethod(method);
        conn.setRequestProperty("User-Agent", "Java");

        conn.connect();//发起连接
        InputStream input = conn.getInputStream();
        InputStreamReader inputReader = new InputStreamReader(input);
        BufferedReader reader = new BufferedReader(inputReader);
        StringBuffer sb = new StringBuffer();
        char[] buf=new char[512];
        int read=0;
        while ((read = reader.read(buf)) >0) {
            sb.append(buf,0,read);
        }
        reader.close();
        inputReader.close();
        input.close();
        String text = sb.toString();
        return new SimpleResponse(conn.getResponseCode(), text);
    }

}
